package com.me.jingdongshop.ui.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.me.common.base.Global;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.LimitbuyBean;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.activity.GoodsDetailActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuer on 2017/5/8/008.
 * 限时抢购类的适配器
 */

public class HomeFalshSaleAdapter extends RecyclerView.Adapter<HomeFalshSaleAdapter.ViewHolder>{
     private RecyclerView recyclerView;
     public List<LimitbuyBean.ProductListBean> mMyProductListBean=new ArrayList<>();
    public HomeFalshSaleAdapter(List<LimitbuyBean.ProductListBean> myProductListBean){
        super();
        this.mMyProductListBean=myProductListBean;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_rv_flash_sale_item, viewGroup, false);
        HomeFalshSaleAdapter.ViewHolder vh=new HomeFalshSaleAdapter.ViewHolder(view);
        return vh;
    }

    public void onBindViewHolder(HomeFalshSaleAdapter.ViewHolder holder, final int position) {
        RecyclerView.ViewHolder viewHolder = holder;
        String pic = mMyProductListBean.get(position).getPic();
        Picasso.with(Global.mContext).load(IHttpService.HOST_URL+pic).into(holder.ivFlashSaleImg);
        String name = mMyProductListBean.get(position).getName();
        holder.tvFlashSaleName.setText(name);
        int limitPrice = mMyProductListBean.get(position).getLimitPrice();
        holder.tvlimitPrice.setText("￥"+limitPrice);
        //删除线
        holder.tvprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        int price = mMyProductListBean.get(position).getPrice();
        holder.tvprice.setText("￥"+price);

        //调转到商品详情
        holder.llHomeFalshSale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), GoodsDetailActivity.class);
                intent.putExtra("id",mMyProductListBean.get(position).getId());
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMyProductListBean==null?0:mMyProductListBean.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llHomeFalshSale;
        private ImageView ivFlashSaleImg;
        private TextView tvFlashSaleName;
        private TextView tvlimitPrice;
        private TextView tvprice;

        public ViewHolder(View view) {
            super(view);
            llHomeFalshSale = (LinearLayout) view.findViewById(R.id.ll_home_flash_sale);
            ivFlashSaleImg = (ImageView) view.findViewById(R.id.iv_flash_sale_img);
            tvFlashSaleName = (TextView) view.findViewById(R.id.tv_flash_sale_name);
            tvlimitPrice = (TextView) view.findViewById(R.id.tv_limitPrice);
            tvprice = (TextView) view.findViewById(R.id.tv_price);

        }
    }
}
